package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class NewInstanceDemo {
  public static void main(String[] args) {
    try{
      Class c = Class.forName("java.lang.Integer");
      System.out.println("Integer class : " + c.getName());
      Constructor<String> constructor = c.getConstructor(String.class);
      Object o = constructor.newInstance("100"); 
      //Integer i = new Integer("100");
      System.out.println("Object's value : " + o);
      
      //Using Class.newInstance
      c = Class.forName("java.lang.String");
      System.out.println("String class : " + c.getName());
      o = c.newInstance();
      System.out.println("Object's value : [" + o + "]");
    }
    catch (ClassNotFoundException e){
      e.printStackTrace();
    }
    catch (InstantiationException e){
      e.printStackTrace();
    }
    catch (IllegalAccessException e){
      e.printStackTrace();
    }
    catch (IllegalArgumentException e){
      e.printStackTrace();
    }
    catch (InvocationTargetException e){
      e.printStackTrace();
    }
    catch (NoSuchMethodException e){
      e.printStackTrace();
    }
    catch (SecurityException e){
      e.printStackTrace();
    }
  }
}
